package com.dianpingmedia.simulate.service.impl;

import com.dianpingmedia.common.BaseSendQueue;
import com.dianpingmedia.common.ErrorCode;
import com.dianpingmedia.common.utils.SmlUtils;
import com.dianpingmedia.simulate.controller.SimulateController;
import com.dianpingmedia.simulate.mapper.SimulateMapper;
import com.dianpingmedia.simulate.model.*;
import com.dianpingmedia.simulate.service.SimulateService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Executors;

@Service
public class SimulateServiceImpl implements SimulateService, InitializingBean {
    @Autowired
    SimulateMapper simulateMapper;

    @Override
    public SimulateUploadEntity.Response uploadMaterial(SimulateUploadEntity.Request request) {
        SimulateUploadEntity.Response response = new SimulateUploadEntity.Response();
        try {
            request.setMaterialType(SmlUtils.getMaterialType(request.getUrl()));
            SimulateQueryEntity queryArgs = new SimulateQueryEntity();
            queryArgs.setMd5(request.getMd5());
            queryArgs.setType(request.getType());
            List<SimulateUploadEntity.Request> requests = simulateMapper.queryMaterial(queryArgs);
            if (requests.size() > 0) {
                response.setErrorCode(SimulateController.BASE_CODE + ErrorCode.MATERIAL_EXIST.getValue());
                response.setMessgae("THE MATERIAL IS EXIST");
                response.setId(requests.get(0).getId());
                return response;
            }

            if (simulateMapper.uploadMaterial(request) > 0) {
                response.setErrorCode(ErrorCode.OK.getValue());
                response.setId(request.getId());
                return response;
            }
            response.setErrorCode(ErrorCode.SQL_ERROR.getValue());
        } catch (Exception e) {
            response.setErrorCode(ErrorCode.SQL_ERROR.getValue());
            response.setMessgae(e.getMessage());
        }
        return response;
    }

    @Override
    public Integer recordPlan(SimulatePlanEntity.Request request) {
        return simulateMapper.recordPlan(request);
    }

    @Override
    public List<DPMaterialEntity> queryMaterial(List<String> materialIds) {
        return simulateMapper.queryMaterialForIds(materialIds);
    }

    @Override
    public SimulateUploadEntity.Request queryRandomMaterial(Integer materialType, Integer dspId) {
        return simulateMapper.queryRandomMaterial(materialType, dspId);
    }

    @Override
    public SimulateArrayResponse<SimulateUploadEntity.Request> queryMaterial(SimulateQueryEntity queryRequest) {
        SimulateArrayResponse<SimulateUploadEntity.Request> response = new SimulateArrayResponse<SimulateUploadEntity.Request>();
        if (queryRequest == null) {
            queryRequest = new SimulateQueryEntity();
        }
        try {
            List<SimulateUploadEntity.Request> requests = simulateMapper.queryMaterial(queryRequest);
            return response.setErrorCode(ErrorCode.OK.getValue())
                    .setData(requests);
        } catch (Exception e) {
            response.setErrorCode(ErrorCode.SQL_ERROR.getValue())
                    .setMessage(e.getMessage());
        }
        return response;
    }

    private BaseSendQueue<SimulateImpressArg> impressArgSendQueue = new BaseSendQueue<SimulateImpressArg>();
    private BaseSendQueue<SimulateNotes> simulateNotesSendQueue = new BaseSendQueue<SimulateNotes>();

    @Override
    public void recordImpress(SimulateImpressArg impressArg) {
        impressArgSendQueue.enqueue(impressArg);
    }

    @Override
    public void recordNotes(SimulateNotes simulateNotes) {
        simulateNotesSendQueue.enqueue(simulateNotes);
    }

    @Override
    public SimulateResponse deleteMaterial(SimulateDeleteEntity simulateDeleteEntity) {
        Integer deleteNum = simulateMapper.deleteMaterial(simulateDeleteEntity.isAll(), simulateDeleteEntity.getIds());
        SimulateResponse simulateResponse = new SimulateResponse();
        if (deleteNum > 0 || simulateDeleteEntity.isAll()) {
            return simulateResponse.setErrorCode(ErrorCode.OK.getValue());
        } else {
            return simulateResponse.setErrorCode(SimulateController.BASE_CODE + ErrorCode.NOT_FOUND.getValue()).setMessage("THE IDS NO FOUND");
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        List<SimulateImpressArg> simulateImpressArgs = impressArgSendQueue.dequeueWait(50, 30000);
                        simulateMapper.recordImpress(simulateImpressArgs);
                    } catch (Exception e) {
                    }
                }
            }
        });

        Executors.newSingleThreadExecutor().execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        List<SimulateNotes> simulateNotes = simulateNotesSendQueue.dequeueWait(50, 30000);
                        simulateMapper.recordNotes(simulateNotes);
                    } catch (Exception e) {
                    }
                }
            }
        });
    }
}
